<!-- Advance table -->
<script src="<?= $url_styles ?>js/plugins/tables/jquery.dataTables.min.js"></script>
<script src="<?= $url_styles ?>js/plugins/tables/colResizable-1.3.min.js"></script>
<script src="<?= $url_styles ?>js/plugins/tables/jquery.tablesorter.min.js"></script> 

<script src="<?= $url_styles ?>js/plugins/formselements/jquery.ibutton.min.js"></script>  
<script src="<?= $url_styles ?>js/plugins/formselements/ui.spinner.min.js"></script> 
<script>
	var page=1;
	function load_table()
	{
		$.ajax({
			url: '<?= url_create('admin', 'image', 'read') ?>',
			dataType: 'json',
			type: 'post',
			data: {p: page},
			success: function(o)
			{
				if(o.state=='success'){
					var str='';
					for(var i in o.data){
						var r=o.data[i];
						var row_class = (o.data[i].position==0)?'X':'A';
						if(i%2==1) row_class+=" odd";
						else row_class+=" even"
						str+='<tr id="row_'+r.id+'" class="grade'+row_class+'">'+
							'<td><image style="max-height:100px" src="'+r.thumb+'" alt="" /><br/>'+r.link+'</td>'+
							'<td>'+r.position+'</td>'+
							'<td class="center"><a href="'+ r.edit_link+'" class="btn btn-mini btn-success row_edit"><?= lang('edit') ?></a>&nbsp;<button class="btn btn-mini btn-danger row_delete" data="'+r.id+'"><?= lang('delete') ?></button></td>'+
							'</tr>';
					}
					if(typeof(oTable)!='undefined') oTable.fnDestroy();
					$('#table tbody').html(str);
					var p = o.pagination;
					var p_numbers = p.left?'...':'';
					for(var i in p.pnumbers){
						var _class = p.pnumbers[i]==p.curr?'active':'button';
						p_numbers+='<a data="'+p.pnumbers[i]+'" class="paginate_'+_class+'">'+p.pnumbers[i]+'</a>';
					}
					if(p.right) p_numbers+='...';
					$('#table_paginate span').html(p_numbers);
					$('#table_previous').attr('data',p.prev);
					$('#table_next').attr('data',p.next);
					$('#table_first').attr('data',p.first);
					$('#table_last').attr('data',p.last);
					   
				}
			}
		});
	}
	$('#table_paginate a').live('click',function(e){
		var p = $(this).attr('data');
		if(p!=null){
			page=p;
			load_table();
		}
	});
	$('.row_delete').live('click',function(e){
		if(confirm('<?= lang('delete_confirm') ?>'))
		{
			$.ajax({
				url: '<?= url_create('admin', 'image', 'delete') ?>',
				type: 'post' ,
				dataType: 'json',
				data:{id: $(this).attr('data')},
				success:function(o)
				{
					if(o.state == 'success')
						$('#row_'+o.id).fadeOut(500);
					else
						$.jGrowl(o.message, {
							header: o.title,
							theme: "notification_styled_"+o.state 
						});
				}
			});
		}
	});

	$('.row_edit').live('click',function(e){
		$('#modal').attr('flag','edit');
		$('#modal').attr('data_id',$(this).attr('data'));
        window.location = $(this).attr('href');

	});

	$('.data_create').live('click',function(e){
		window.location = '<?= url_create('admin', 'image', 'create') ?>';
	});
		
	$('.data_update').live('click',function(e){
		$.ajax({
			url: '<?= url_create('admin', 'image', 'update_all') ?>',
			dataType: 'json',
			success: function(o){
				if(o.state=='success'){
					load_table();
					$.jGrowl(o.message, {
						header: o.title,
						theme:"notification_styled_"+o.state 
					});
				}
				else{
					alert(o.message);
				}
						
			}
		})
	})
	$('#modal_ok').live('click',function(e){
		$('.modal-form').submit();
	});
	$('.modal-form').submit(function(e){
		var flag = $('#modal').attr('flag');

		$.ajax({
			url: flag=='create'?'<?= url_create('admin', 'image', 'create') ?>':'<?= url_create('admin', 'image', 'update') ?>',
			dataType: 'json',
			type: 'post',
			data: {
				id: $('#modal').attr('data_id'),
				name: $('#name').attr('value'),
				permission: $('#permission').attr('value'),
				description: $('#description').val()
			},
			success: function(o){
				if(o.state=='success'){
					load_table();
					$('#modal_toggle').click();
					$.jGrowl(o.message, {
						header: o.title,
						theme:"notification_styled_"+o.state 
					});
				}else{
					alert(o.message);
				}
					
			}
		});
		return false;
	});
		
	$('#modal_cancel').live('click',function(e){
		$('#modal_toggle').click();
	});
	$(document).ready(load_table());
</script>
